//
//  Status.swift
//  sinaWeibo
//
//  Created by 浅爱 on 16/3/19.
//  Copyright © 2016年 y. All rights reserved.
//

import UIKit
import AFNetworking


class Status: NSObject {
    
    // 属性
    // 微博创建时间
    var created_at: String?
    
    // 微博ID
    // swift中Int和OC中NSInteger一样  Int64和oc中longlong一样
    var id: Int64 = 0
    
    // 微博信息内容
    var text: String?
    
    //微博来源
    var source: String?
    
    // 添加User
    var user: User?
    
    init(dict: [String: AnyObject]) {
        
        super.init()
        
        setValuesForKeysWithDictionary(dict)
        
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        
        // 对 user 做特殊处理
        if key == "user" {
            
            guard let dict = value as? [String: AnyObject] else {
            
                return
            
            }
            
            // 字典转模型
            user = User(dict: dict)
            
            // 必须return 防止下面super回到字典
            return
        }
        
        super.setValue(value, forKey: key)
        
    }
    
    
    // 未找到的key
    override func setValue(value: AnyObject?, forUndefinedKey key: String) { }

    // MARK: 重写描述方法
    override var description: String {
    
        let keys = ["id", "text", "created_at", "source"]

        let dict = self.dictionaryWithValuesForKeys(keys)
        
        
        return dict.description
        
    }
    
}





// 网络访问
extension Status {

    class func loadHomeData(finished: (array: [Status]?) -> ()) {
        
        guard let token = userAccount.sharedAccount.access_token else {
        
            
            return
        
        }
        
        let urlStr = "https://api.weibo.com/2/statuses/home_timeline.json"
        
        let AFN = AFHTTPSessionManager()
        
        let parameters = ["access_token": token]
        
        AFN.GET(urlStr, parameters: parameters, progress: nil, success: { (_, result) -> Void in
            
            // 数据转成字典
            // 极大减少分支结构
            guard let dict = result as? [String: AnyObject] else {
            
                finished(array: nil)
                
                print("数据结构错误1")
                
            
                return
                
            }
            
            guard let array = dict["statuses"] as? [[String: AnyObject]] else {
            
                finished(array: nil)
                
                print("数据结构错误2")
                
                return
            
            }

            var tmpArray = [Status]()
            
            // 遍历数组
            for item in array {
            
                // 字典转模型
                let s = Status(dict: item)
                
                // 添加到数组
                tmpArray.append(s)
                
                print(s)
            
            }
            
            // 刷新界面
            finished(array: tmpArray)
            
            
            
            }) { (_, error) -> Void in
                
                finished(array: nil)
                
                print(error)
                
        }
        
    }


}